package cn.niit.utils;

import java.util.Random;

public class SaltUtils {

    /** 公共盐 */
    public static final String PUBLIC_SALT = "Slpc6Hx0zjt1x&Rd";

    /**
     * 密钥默认长度
     */
    private static final int DEFAULT_LENGHT = 16;

    public static String getSalt(){
        return getSalt(DEFAULT_LENGHT);
    }

    /**
     * 生成salt的静态方法
     * @param n
     * @return
     */
    public static String getSalt(int n){
        if(n <= 0){
            n = DEFAULT_LENGHT;
        }
        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890!@#$%^&*()".toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            char aChar = chars[new Random().nextInt(chars.length)];
            sb.append(aChar);
        }
        return sb.toString();
    }
}